/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.module.background.controller;

import cn.hutool.core.util.ReflectUtil;
import com.github.lvyanyang.annotation.Authorize;
import com.github.lvyanyang.core.BaseWebController;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.Const;
import com.github.lvyanyang.core.XCI;
import com.github.lvyanyang.exceptions.BusinessException;
import com.xci.edu.core.entity.EduCourseware;
import com.xci.edu.core.filter.EduCoursewareFilter;
import com.xci.edu.core.service.EduCoursewareService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 课程信息页面控制器
 *
 * @author 焦元义 jiaoyuanyi@xci96716.com
 * @since 2020-11-24 15:14:37
 */
@Authorize
@Controller
@RequestMapping(value = "/background/eduCourseware")
public class EduCoursewareController extends BaseWebController {
    /**
     * 课程信息服务
     */
    @Resource
    private EduCoursewareService eduCoursewareService;

    //region 页面

    /**
     * 首页
     */
    @GetMapping
    //@Authorize(code = "background.eduCourseware")
    public String index() {
        return "background/eduCourseware/index";
    }

    /**
     * 新建页
     */
    @GetMapping("/create")
    //@Authorize(code = "background.eduCourseware.insert")
    public String create(ModelMap map) {
        createMark(map);
        var entity = new EduCourseware();
        entity.setId(XCI.nextId());
        entity.setPath(eduCoursewareService.selectCount()+1);
        map.put("entity", entity);
        return "background/eduCourseware/edit";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit")
    //@Authorize(code = "background.eduCourseware.update")
    public String edit(String id, ModelMap map) {
        var entity = eduCoursewareService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduCourseware/edit";
    }

    /**
     * 详情页
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = eduCoursewareService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduCourseware/details";
    }

    /**
     * 表格查询
     */
    @ResponseBody
    @PostMapping("/grid")
    //@Authorize(code = "background.eduCourseware.select")
    public BoolResult grid(EduCoursewareFilter filter) {
        return BoolResult.success(eduCoursewareService.selectPageList(filter));
    }

    /**
     * 新建保存
     */
    @ResponseBody
    @PostMapping("/createSave")
    //@Authorize(code = "background.eduCourseware.insert")
    public BoolResult createSave(@ModelAttribute EduCourseware entity) {
        return eduCoursewareService.insert(entity);
    }

    /**
     * 修改保存
     */
    @ResponseBody
    @PostMapping("/updateSave")
    //@Authorize(code = "background.eduCourseware.update")
    public BoolResult updateSave(@ModelAttribute EduCourseware entity) {
        return eduCoursewareService.update(entity);
    }

    /**
     * 删除
     */
    @ResponseBody
    @PostMapping("/delete")
    //@Authorize(code = "background.eduCourseware.delete")
    public BoolResult delete(String ids) {
        return eduCoursewareService.delete(ids);
    }

    /**
     * 修改状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "background.eduCourseware.updateStatus")
    public BoolResult status(String id, Integer status) {
        eduCoursewareService.updateStatus(id, XCI.toBool(status));
        return BoolResult.success();
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    //@Authorize(code = "background.eduCourseware.export")
    public void export(EduCoursewareFilter filter) {
        XCI.exportExcel(eduCoursewareService.selectList(filter), EduCourseware.class, "课程信息列表");
    }
}
